package com.chandon.juc.thread_communication.AddStringsToList;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 描述
 *
 * @author Dong
 */

public class UseCountDownLatch {
	static CountDownLatch countDownLatch = new CountDownLatch(1);
	static List<String> list = new ArrayList<>(10);

	/**
	 * 3. CountDownLatch =0 所有线程释放
	 */
	public static void CountDownLatch() {


		new Thread(() -> {
			for (int i = 0; i < 10; i++) {
				list.add("abc");
				System.out.println("线程A list.size=" + list.size());

				if (list.size() == 4) {
					System.out.println("线程B要开始了");
					countDownLatch.countDown();
				}
			}
		}).start();


		new Thread(() -> {
			if (list.size() != 4) {
				try {
					// 阻塞到计数器=0
					countDownLatch.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("线程B开始执行");
		}).start();


	}

}
